package com.jettech.oa.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.jettech.oa.common.ConstType;
import com.jettech.oa.utils.DateUtil;
/**
 * 公文的自定义表单
 * @author Administrator
 *
 */
@Entity
@Table(name="jettech_document_property",
    uniqueConstraints = {@UniqueConstraint(columnNames={"document_id", "document_property_name"})})
public class DocumentProperty implements Serializable {
	
	private static final long serialVersionUID = -3183431095491297021L;
	private Long id;//主键
	private Document document;//公文主键
	private String name;//自定义表单属性名称
	private String value;//自定义表单属性名称对应值
	private String type;//表单的类型,例如:字符串,整形,日期,Long ,Float,文件等
	private Long formPropertyId;

	public DocumentProperty(){
		
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="document_property_id")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="document_id")
	public Document getDocument() {
		return document;
	}
	public void setDocument(Document document) {
		this.document = document;
	}
	@Column(name="document_property_name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="document_property_value")
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	@Column(name="document_property_type")
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	@Column(name="form_property_id")
	public Long getFormPropertyId() {
		return formPropertyId;
	}

	public void setFormPropertyId(Long formPropertyId) {
		this.formPropertyId = formPropertyId;
	}
	public Object value(){
		if(ConstType.OBJECT_TYPE_INTEGER.equals(type)){
			if(value!=null && !"".equals(value.trim())){
				return new Integer(value.trim());
			}else{
				return 0;
			}
		}else if(ConstType.OBJECT_TYPE_DATE.equals(type)){
			if(value!=null && !"".equals(value.trim())){
				return DateUtil.dateFormat.format(value.trim());
			}else{
				return new Date();
			}
			
		}else if(ConstType.OBJECT_TYPE_STRING.equals(type)){
			if(value!=null && !"".equals(value.trim())){
				return value.trim();
			}else{
				return value;
			}
		}else if(ConstType.OBJECT_TYPE_LONG.equals(type)){
			if(value!=null && !"".equals(value.trim())){
				return new Long(value.trim());
			}else{
				return 0l;
			}
		}else if(ConstType.OBJECT_TYPE_FLOAT.equals(type)){
			if(value!=null && !"".equals(value.trim())){
				return new Float(value.trim());
			}else{
				return 0f;
			}
		}else{
			return value;
		}
	}
	public boolean equals(Object other){
		return EqualsBuilder.reflectionEquals(this, other);
	}
	public int hashCode(){
		return HashCodeBuilder.reflectionHashCode(this);
	}
	public String toString(){
		return ToStringBuilder.reflectionToString(this);
	}
}
